package com.example.myapplication.Caringwelfare;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.BenefitBean;
import com.example.myapplication.ShuJu.WelfareclassificationBean;
import com.example.myapplication.apter.BenefitApter;
import com.example.myapplication.apter.WelfareclassificationApter;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class BenefitActivity extends AppCompatActivity {

    private EditText searchEditText;
    private RecyclerView re_bene;
    private Button nextPageButton;
    private Button previousPageButton;
    private OkHttpClient okHttpClient;
    private Gson gson;
    private Handler handler;
    private Request request;
    private String listID;
    private BenefitBean benefitBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_benefit);
        Intent intent = getIntent();
        listID = intent.getStringExtra("listID");
        iniyview();
        //根据id跳转到不同的详情
        Benefit();
    }

    private void iniyview() {
        searchEditText = findViewById(R.id.searchEditText);
        re_bene = findViewById(R.id.re_bene);
        previousPageButton = findViewById(R.id.previousPageButton);
        nextPageButton = findViewById(R.id.nextPageButton);
        okHttpClient = new OkHttpClient();
        gson = new Gson();
        handler = new Handler();
    }

    public void  Benefit() {
        request =  new Request.Builder()
                .url("http://124.93.196.45:10001/prod-api/api/public-welfare/public-welfare-activity/list?typeId="+listID+"&pageNum=1&pageSize=10")
                .addHeader("Authorization", TokenBean.token)
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                e.printStackTrace();
                Log.d("xx","bb");
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String json =  response.body().string();
                Log.d("xjson",json);
                gson = new Gson();
                benefitBean = gson.fromJson(json, BenefitBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run(){
                        binBenefit();
                    }
                });
            }
        });
    }

    private void binBenefit() {
        //适配器
        BenefitApter benefitApter = new BenefitApter(BenefitActivity.this,benefitBean.getRows());
        LinearLayoutManager linearLayout= new LinearLayoutManager(BenefitActivity.this);
        re_bene.setLayoutManager(linearLayout);
        re_bene.setAdapter(benefitApter);
    }
}